<?php
/*
121.199.53.221 coin.mall.ffrj.net
121.199.53.221 manager.mall.ffrj.net
121.199.53.221 shop.mall.ffrj.net
121.199.53.221 center.mall.ffrj.net
121.199.53.221 shop-web.mall.ffrj.net
 */
define('COIN_DOMAIN', 'coin.mall.ffrj.net');
define('SHOP_DOMAIN', 'shop.mall.ffrj.net');

/**
 * 商城类api
 * 
 * Usage
 * $api = MallApi::instance();
 * // 附上uid, token
 * $api->attachUser('12344555', '8e16992c2323');
 * 
 * // 调用签到
 * $coin = $api->getUserCoin();
 * if (!$coin) {
 * 		echo $api->last_error->message;
 * 		return;
 * }
 * 
 * // 正常操作
 * ...
 * 
 * @author Administrator
 *
 */
class MallApi {
	
	private static $_instance;
	
	public static function instance() {
		if (!self::$_instance) {
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
	/**
	 * @var APIError
	 */
	public $last_error;
	
	protected $userAuth;
	
	private function __construct() {}
	
	public function attachUser($uid, $token) {
		$this->userAuth = new UserAuth($uid, $token);
	}
	
	/**
	 * @return array 正常
	 * @return false 错误，请使用last_error获得详细错误
	 */
	public function getCoinRuleList()
	{
		return $this->reqeust(COIN_DOMAIN, 'rule/list');
	}
	
	public function getCoinTaskList()
	{
		return $this->reqeust(COIN_DOMAIN, 'task/list');
	}
	
	public function executeCoinTask($trigger)
	{
		return $this->reqeust(COIN_DOMAIN, 'task/execute', array('trigger'=>$trigger));
	}
	
	public function getUserCoin()
	{
		return $this->reqeust(COIN_DOMAIN, 'user/coin');
	}
	
	private function reqeust($domain, $uri, $params = array()) {
		if (!$this->userAuth) {
			throw new Exception('need attachUser first');
		}
		
		$url = "http://{$domain}/{$uri}";
		$params = array('uid'=>$this->userAuth->uid, 'token'=>$this->userAuth->token, 'hash'=>$this->userAuth->hash) + $params;
		$body = $this->getHttpResponse($url, $params);
		
		$result = json_decode($body, true);
		if ($result) {
			if ($result['errorNo']) {
				$this->last_error = new APIError($result['errorNo'], $result['errorMsg']);
				return false;
			} else {
				return $result['returnData'];
			}
		}
		

		$this->last_error = new APIError(10000, '服务器内部错误');
		return false;
	}
	
	private function getHttpResponse($url, $params) {
		// 拼装get参数
		$url .= '?'.http_build_query($params);
		
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_HEADER, 0 ); // 过滤HTTP头
		curl_setopt($curl, CURLOPT_USERAGENT, "FFWEB_1.0-API");
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($curl, CURLOPT_TIMEOUT, 30);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);// 显示输出结果
		curl_setopt($curl, CURLOPT_ENCODING, "");
		curl_setopt($curl, CURLOPT_HEADER, FALSE);
		$responseText = curl_exec($curl);
		
		// var_dump( curl_error($curl) );exit;//如果执行curl过程中出现异常，可打开此开关，以便查看异常内容
		curl_close($curl);
		return $responseText;
	}
	
	public static function getHttpResponsePOST($url, $para, $input_charset = '') {
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_HEADER, 0 ); // 过滤HTTP头
		curl_setopt($curl,CURLOPT_POST,true); // post传输数据
		curl_setopt($curl,CURLOPT_POSTFIELDS,$para);// post传输数据
		curl_setopt($curl, CURLOPT_USERAGENT, "FFWEB_1.0-API");
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($curl, CURLOPT_TIMEOUT, 30);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);// 显示输出结果
		curl_setopt($curl, CURLOPT_ENCODING, "");
		curl_setopt($curl, CURLOPT_HEADER, FALSE);
		$responseText = curl_exec($curl);
	
		// var_dump( curl_error($curl) );exit;//如果执行curl过程中出现异常，可打开此开关，以便查看异常内容
		curl_close($curl);
		return $responseText;
	}
}

class UserAuth {
	
	public $uid;
	public $token;
	public $hash;
	
	public function __construct($uid, $token) {
		$this->uid = $uid;
		$this->token = $token;
		$this->hash = $this->generateHash();
	}
	
	public function generateHash()
	{
		// 1000秒内有效
		return sha1($this->uid.$this->token.(substr(time(), 0, -3).'000'));
	}
}

class APIError {
	
	public $code;
	public $message;
	
	public function __construct($code, $message) {
		$this->code = $code;
		$this->message = $message;
	}
}
